package cn.edu.hatu.bookshop.controller;

import cn.edu.hatu.bookshop.entity.Admin;
import cn.edu.hatu.bookshop.entity.Customer;
import cn.edu.hatu.bookshop.service.AdminService;
import cn.edu.hatu.bookshop.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {
    private CustomerService customerService;
    private AdminService adminService;

    @Autowired
    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    @Autowired
    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    @RequestMapping("/adminLogin")
    public String adminPage() {
        return "Admin/MainPage";
    }

    @RequestMapping("/test")
    public String test() {
        return "Customer/test/inputCheck";
    }

    @RequestMapping("/exit")
    public String exit(HttpServletRequest request,HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();//获取cookie数组
        for (Cookie c : cookies   //遍历cookie
        ) {
            if (c.getName().equals("customerId")) {
                System.out.println("cookie:" + c.getName());
                Cookie cookie = new Cookie(c.getName(), null);//删除前必须要new 一个valu为空；
                cookie.setPath("/");//路径要相同
                cookie.setMaxAge(0);//生命周期设置为0
                response.addCookie(cookie);
                break;
            }
        }
        return "redirect:homePage";
    }

    @RequestMapping("/loginInput")
    public String loginInput() {
        return "Customer/login";
    }

    @RequestMapping("/inputCheck")
    public String inputCheck() {
        return "/Customer/inputCheck";
    }

    /*
     * 根据用户选择的登录身份（顾客/管理员），对登录信息进行校验
     * 如果登录信息合法，将用户信息保存在cookie中，前端通过使用
     * js检查cookie信息，根据检查结果显示(未登录/某某已登录)
     * */
    @RequestMapping("/loginCheck")
    public String loginCheck(Model model, HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username").trim();
        String password = request.getParameter("password").trim();
        String identity = request.getParameter("identity").trim();

        System.out.println(identity);
        if (identity.equals("customer")) {
            Customer customer = customerService.loginCheck(username, password);
            if (customer.getCustomerId() != 0) {
                model.addAttribute("customer", customer);
                Cookie[] cookies = request.getCookies();//获取cookie数组
                for (Cookie c : cookies   //遍历cookie
                ) {
                    if (c.getName().equals("username")) {
                        Cookie cookie = new Cookie(c.getName(), null);//删除前必须要new 一个valu为空；
                        /*cookie.setPath("/");//路径要相同*/
                        cookie.setMaxAge(0);//生命周期设置为0
                        break;
                    }
                }
                Cookie name = new Cookie("username", username);
                Cookie customerId = new Cookie("customerId", String.valueOf(customer.getCustomerId()));
                Cookie oldPassword = new Cookie("oldPassword", customer.getPassword());
                System.out.println("oldpassword:" + customer.getPassword());
                System.out.println("cookiepath:" + name.getPath());
                response.addCookie(name);
                response.addCookie(customerId);
                response.addCookie(oldPassword);
                /*return "Customer/test/testChangeNavbar";*/
                return "redirect:homePage";
            } else {
                return "redirect:loginInput";
            }
        } else {
            Admin admin = adminService.getAdmin();
            if (admin.getName().equals(username) && admin.getPassword().equals(password)) {
                return "redirect:adminLogin";
            } else {
                return "redirect:loginInput";
            }
        }
    }
}